/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.JavaXML.DOMValidator;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Nodes;
import nux.xom.xquery.XQueryUtil;

/**
 *
 * @author thanhtq
 */
public class ReadQuestionAndAnswer implements Serializable {

    public int numberManTl = 0;
    public static final String SURVEY_XML = "D:\\Project\\thanhtv00011\\Survey\\Survey.xml";

    public List<Question> readQuestion(String path, String id) {
        List<Question> questions = new ArrayList<Question>();
        Question question;
        try {
            Document doc = new Builder().build(new File(SURVEY_XML));
            Nodes nodes = XQueryUtil.xquery(doc, "//Survey/categorys/category/questions/question");
            for (int i = 0; i < nodes.size(); i++) {
                    question = new Question();
                    question.setId(nodes.get(i).query("@id").get(0).getValue());
                    question.setContent(nodes.get(i).query("Content").get(0).getValue());
                    questions.add(question);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return questions;
    }

    public List<Answer> readAnswer(String path, String id) {
        List<Answer> answers = new ArrayList<Answer>();
        numberManTl = 0;
        Answer answer;
        try {
            Document doc = new Builder().build(new File(path));
            Nodes nodes = XQueryUtil.xquery(doc, "//Survey/categorys/category/answers/answer");
            for (int i = 0; i < nodes.size(); i++) {
                answer = new Answer();
                answer.setId(nodes.get(i).query("@id").get(0).getValue());
                answer.setOption(nodes.get(i).query("option").get(0).getValue());
                answer.setQuesId(nodes.get(i).query("QuesID").get(0).getValue());
                answer.setRate(nodes.get(i).query("rate").get(0).getValue());
                numberManTl += Integer.parseInt(answer.getRate());
                answers.add(answer);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return answers;
    }

    public int getNumberManTl() {
        return numberManTl;
    }

    public void setNumberManTl(int numberManTl) {
        this.numberManTl = numberManTl;
    }
}
